

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="caoyusang">
  <meta name="keywords" content="">
  <title>ThreadLocal详解 - caoyusang的科研日常</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>caoyusang</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/7.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-04-06 20:19" pubdate>
        2021年4月6日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      179 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      2
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">ThreadLocal详解</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2 个月前
                
              </p>
            
            <div class="markdown-body" id="post-body">
              <h2 id="ThreadLocal"><a href="#ThreadLocal" class="headerlink" title="ThreadLocal"></a>ThreadLocal</h2><p>ThreadLocal 主要用来提供线程局部变量，将参数储存在线程中，然后在该线程运行的任何阶段都能从线程中获取。多线程环境下，每个线程都可以通过<code>set()</code>和<code>get()</code>来对本线程局部变量进行操作，不会和其他线程的局部变量发生冲突，<strong>实现了线程间的数据隔离</strong>。</p>
<h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><img src="/2021/04/06/ThreadLocal%E8%AF%A6%E8%A7%A3/image-20210324150915632.png" srcset="/img/loading.gif" class="">
<ul>
<li><p>每一个Thread对象都包含一个ThreadLocalMap类型的<code>成员变量threadLocals</code>，它存储了本线程中所有的ThreadLocal对象及其对应的值，即一个ThreadLocalMap对象。</p>
<pre><code class="hljs java"><span class="hljs-comment">/* ThreadLocal values pertaining to this thread. This map is maintained</span>
<span class="hljs-comment">* by the ThreadLocal class. */</span>
ThreadLocal.ThreadLocalMap threadLocals = <span class="hljs-keyword">null</span></code></pre>
</li>
<li><p>ThreadLocalMap由一个个的Entry对象构成，Entry继承自weakReference<ThreadLocal<?>&gt;，一个Entry由ThreadLocal对象以及线程局部变量Object组成，其中ThreadLocal作为key，对应的线程变量Object作为value。</p>
<img src="/2021/04/06/ThreadLocal%E8%AF%A6%E8%A7%A3/20180920212201150.png" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadLocalMap</span> </span>&#123;

        <span class="hljs-comment">/**</span>
<span class="hljs-comment">         * The entries in this hash map extend WeakReference, using</span>
<span class="hljs-comment">         * its main ref field as the key (which is always a</span>
<span class="hljs-comment">         * ThreadLocal object).  Note that null keys (i.e. entry.get()</span>
<span class="hljs-comment">         * == null) mean that the key is no longer referenced, so the</span>
<span class="hljs-comment">         * entry can be expunged from table.  Such entries are referred to</span>
<span class="hljs-comment">         * as &quot;stale entries&quot; in the code that follows.</span>
<span class="hljs-comment">         */</span>
        <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Entry</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WeakReference</span>&lt;<span class="hljs-title">ThreadLocal</span>&lt;?&gt;&gt; </span>&#123;
            <span class="hljs-comment">/** The value associated with this ThreadLocal. */</span>
            Object value;

            Entry(ThreadLocal&lt;?&gt; k, Object v) &#123;
                <span class="hljs-keyword">super</span>(k);
                value = v;
            &#125;
        &#125;
        <span class="hljs-comment">//....很长</span>
&#125;</code></pre>
</li>
</ul>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><p>ThreadLocal提供<code>set()</code>方法和<code>get()</code>方法操作线程局部变量。</p>
<p>每一个线程的ThreadLocalMap容器各自私有，这些容器相互独立互不影响，不会存在线程安全性的问题。</p>
<ul>
<li><p>set()</p>
<ul>
<li>ThreadLocal获取当前线程对象</li>
<li>获取当前线程的ThreadLocalMap对象</li>
<li>以当前的ThreadLocal为key，将线程局部变量值存进ThreadLocalMap对象中</li>
</ul>
<pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">set</span><span class="hljs-params">(T value)</span> </span>&#123;

    <span class="hljs-comment">// 得到当前线程对象</span>
    Thread t = Thread.currentThread();

    <span class="hljs-comment">// 这里获取ThreadLocalMap</span>
    ThreadLocalMap map = getMap(t);

    <span class="hljs-comment">// 如果map存在，则将当前的ThreadLocal对象作为key，要存储的对象作为value存到map里面去</span>
    <span class="hljs-keyword">if</span> (map != <span class="hljs-keyword">null</span>)&#123;
        map.set(<span class="hljs-keyword">this</span>, value);
    &#125; <span class="hljs-keyword">else</span>&#123;		<span class="hljs-comment">//map不存在，调用createMap()方法为当前的线程创建一个ThreadLocalMap</span>
        createMap(t, value);
    &#125;
&#125;</code></pre>
</li>
<li><p>createMap() —— 创建并初始化线程的ThreadLocalMap</p>
<pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">createMap</span><span class="hljs-params">(Thread t, T firstValue)</span> </span>&#123;
    t.threadLocals = <span class="hljs-keyword">new</span> ThreadLocalMap(<span class="hljs-keyword">this</span>, firstValue);
&#125;</code></pre>
</li>
<li><p>get()</p>
<ul>
<li>ThreadLocal获取当前线程对象</li>
<li>获取当前线程的ThreadLocalMap对象</li>
<li>以当前的ThreadLocal为key，获取对应的value</li>
</ul>
<pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> T <span class="hljs-title">get</span><span class="hljs-params">()</span> </span>&#123;
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    <span class="hljs-keyword">if</span> (map != <span class="hljs-keyword">null</span>) &#123;
        ThreadLocalMap.Entry e = map.getEntry(<span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">if</span> (e != <span class="hljs-keyword">null</span>) &#123;
            <span class="hljs-meta">@SuppressWarnings(&quot;unchecked&quot;)</span>
            T result = (T)e.value;
            <span class="hljs-keyword">return</span> result;
        &#125;
    &#125;
    <span class="hljs-keyword">return</span> setInitialValue();
&#125;</code></pre>
</li>
</ul>
<h3 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h3><ul>
<li>进行对象跨层传递时，使用ThreadLocal避免参数/对象的多次传递，打破层次间的约束</li>
<li>线程间数据隔离</li>
<li>解决Spring中单例Bean的线程安全问题</li>
<li>进行事务操作，用于存储线程的事务信息<ul>
<li>spring声明式事务的重要实现基础就是ThreadLocal</li>
</ul>
</li>
<li><code>数据库连接管理</code>、Session会话管理</li>
</ul>
<p><code>数据库连接管理</code>：</p>
<pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DBUtil</span> </span>&#123;
    <span class="hljs-comment">//数据库连接池</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> BasicDataSource source;

    <span class="hljs-comment">//为不同的线程管理连接</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ThreadLocal&lt;Connection&gt; local;


    <span class="hljs-keyword">static</span> &#123;
        ...
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Connection <span class="hljs-title">getConnection</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> SQLException </span>&#123;
        <span class="hljs-comment">//获取Connection对象</span>
        Connection connection = source.getConnection();

        <span class="hljs-comment">//把Connection放进ThreadLocal里面</span>
        local.set(connection);

        <span class="hljs-comment">//返回Connection对象</span>
        <span class="hljs-keyword">return</span> connection;
    &#125;

    <span class="hljs-comment">//关闭数据库连接</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeConnection</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-comment">//从线程中拿到Connection对象</span>
        Connection connection = local.get();

        <span class="hljs-keyword">try</span> &#123;
            <span class="hljs-keyword">if</span> (connection != <span class="hljs-keyword">null</span>) &#123;
                <span class="hljs-comment">//恢复连接为自动提交</span>
                connection.setAutoCommit(<span class="hljs-keyword">true</span>);

                <span class="hljs-comment">//这里不是真的把连接关了,只是将该连接归还给连接池</span>
                connection.close();

                <span class="hljs-comment">//既然连接已经归还给连接池了,ThreadLocal保存的Connction对象也已经没用了</span>
                local.remove();

            &#125;
        &#125; <span class="hljs-keyword">catch</span> (SQLException e) &#123;
            e.printStackTrace();
        &#125;
    &#125;


&#125;</code></pre>
<h3 id="内存泄漏"><a href="#内存泄漏" class="headerlink" title="内存泄漏"></a>内存泄漏</h3><p>不会再被使用的对象或者变量占用的内存不能被回收，就说明发生了内存泄漏。随着内存泄漏次数的累积，无论计算机有多少内存，迟早被耗光，会出抛出OOM(OutOfMemoryError)异常，程序异常终止。</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">类型</th>
<th style="text-align:center">回收时间</th>
<th style="text-align:center">应用场景</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">强引用</td>
<td style="text-align:center">一直存活，除非GC Roots不可达</td>
<td style="text-align:center">所有程序的场景，基本对象，自定义对象等</td>
</tr>
<tr>
<td style="text-align:center">软引用</td>
<td style="text-align:center">内存不足时会被回收</td>
<td style="text-align:center">一般用在对内存非常敏感的资源上，用作缓存的场景比较多</td>
</tr>
<tr>
<td style="text-align:center">弱引用</td>
<td style="text-align:center">只能存活到下一次GC前</td>
<td style="text-align:center">生命周期很短的对象，例如ThreadLocal中的Key。</td>
</tr>
<tr>
<td style="text-align:center">虚引用</td>
<td style="text-align:center">随时会被回收</td>
<td style="text-align:center">可能被JVM团队内部用来跟踪JVM的垃圾回收活动</td>
</tr>
</tbody>
</table>
</div>
<p>ThreadLocalMap使用ThreadLocal的弱引用作为key时，如果一个ThreadLocal不存在外部强引用了，那么这个作为key的ThreadLocal必然在下一次GC中被回收。<code>当key成为null后</code>，下一次ThreadLocalMap调用set()、get()、remove()方法时，会清除value的值。</p>
<p><code>ThreadLocal内存泄漏的根源</code>：</p>
<p>ThreadLocalMap的<code>生命周期</code>跟Thread一样长，如果没有手动删除对应key就会导致内存泄漏，而不是因为弱引用。</p>
<p><code>ThreadLocal的正确使用方法</code>：</p>
<ul>
<li>每次使用完ThreadLocal后，手动remove()。</li>
<li>将ThreadLocal变量设置为private static，这样就一直存在ThreadLocal的强引用，也就能保证任何时候都能通过ThreadLocal的弱引用访问到Entry的value值，进而清除掉。</li>
</ul>
<h3 id="参考博客"><a href="#参考博客" class="headerlink" title="参考博客"></a>参考博客</h3><p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&amp;mid=2247484118&amp;idx=1&amp;sn=da3e4c4cfd0642687c5d7bcef543fe5b&amp;chksm=ebd743d7dca0cac19a82c7b29b5b22c4b902e9e53bd785d066b625b4272af2a6598a0cc0f38e&amp;scene=21#wechat_redirect">ThreadLocal就是这么简单 by Java3y</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/zzg1229059735/article/details/82715741">ThreadLocal理解及应用 by pony-zi</a></p>
<h3 id="参考视频"><a href="#参考视频" class="headerlink" title="参考视频"></a>参考视频</h3><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Eb4y1R7zd?p=25">ThreadLocal的原理以及使用场景</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Eb4y1R7zd?p=26">ThreadLocal内存泄漏问题</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java/">Java</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/JUC/">JUC</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/04/06/MySQL%E7%B4%A2%E5%BC%95%E8%AF%A6%E8%A7%A3/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">MySQL索引详解</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/04/05/Zookeeper%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/">
                        <span class="hidden-mobile">Zookeeper入门学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "ThreadLocal详解&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.staticfile.org/mathjax/3.0.5/es5/tex-svg.js" ></script>

  











</body>
</html>
